package gui.mainWindow.scores;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import core.GuiComponentLabels;

public class ScoresTableModel extends AbstractTableModel {
	private final int columnCount;
	private final ArrayList < Object [] > rowData = new ArrayList < Object [] >();
	private String [] columnNames = new String [this.columnCount];

	public ScoresTableModel() {
		this.columnNames = new String [] { GuiComponentLabels.ROUND, GuiComponentLabels.SCORE, GuiComponentLabels.TENS, GuiComponentLabels.NINES, GuiComponentLabels.XS };
		this.columnCount = this.columnNames.length;
	}

	@Override
	public String getColumnName(int column) {
		return this.columnNames [column];
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		/** first two columns are not editable */
		if ((col == 0)) {
			return false;
		}
		/** last row is not editable */
		if (row == this.getRowCount() - 1) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public int getColumnCount() {
		return this.columnCount;
	}

	@Override
	public int getRowCount() {
		return this.rowData.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.rowData.get(rowIndex) [columnIndex];
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		while (this.getRowCount() < row) {
			this.addRow();
		}
		this.rowData.get(row) [col] = value;
		this.fireTableCellUpdated(row, col);
	}

	public void addRow() {
		this.rowData.add(new Object [this.columnCount]);

	}

	public void setColumnIdentifiers(String [] strings) {
		this.columnNames = strings;
	}

	public void addRow(Object [] objects) {
		this.rowData.add(objects);
	}

	@Override
	public Class getColumnClass(int c) {
		try {
			return this.getValueAt(0, c).getClass();
		} catch (NullPointerException e) {
			return Object.class;
		}
	}

	public Object [] getRowData(int row) {
		return this.rowData.get(row);
	}

	public void removeRow(int row) {
		this.rowData.remove(row);
	}
}
